package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"novel-mng/cache"
	"novel-mng/models"
	"novel-mng/pkj"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	redis_key := "login_err_" + username
	errCount, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if errCount >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误超过三次,禁止登录",
		})
		return
	}
	var user models.User
	err := models.DB.Where("username=?", username).Debug().Find(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户查询错误",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}
	if user.Password != pkj.Md5(password) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if errCount == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误超过三次,请两小时后重试",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
	})
	return

	/*
		username := c.PostForm("username")
		password := c.PostForm("password")
		if username == "" || password == "" {
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "字段不能为空",
			})
			return
		}
		redis_key := "login_err_" + username
		errCount, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
		if errCount >= 3 {
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误超过三次,禁止登录",
			})
			return
		}
		var user models.User
		err := models.DB.Where("username=?", username).Find(&user).Error
		if err != nil {
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "查找用户失败",
			})
			return
		}
		if user.ID == 0 {
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "用户不存在",
			})
			return
		}
		if user.Password != pkj.Md5(password) {
			cache.Rdb.Incr(cache.Ctx, redis_key)
			if errCount == 2 {
				cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
				c.JSON(200, gin.H{
					"code": 500,
					"msg":  "密码错误超过三次,请两小时后重试",
				})
				return
			}
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 200,
			"data": username,
			"msg":  "登录成功",
		})
		return
	*/

}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	var user []models.User
	redis_key := "list_page_" + strconv.Itoa(page)
	cachceData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cachceData == "" {
		models.DB.Offset(offset).Limit(size).Debug().Find(&user)
		josnData, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, redis_key, josnData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cachceData), &user)
	}
	var total int64
	models.DB.Model(&models.User{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"data":  user,
		"msg":   "列表成功",
		"total": total,
	})
	return
}
func Detail(c *gin.Context) {
	id := c.Query("id")
	var user models.User
	redis_key := "detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Where("id=?", id).First(&user)
		josnData, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, redis_key, josnData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &user)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"data": user,
		"msg":  "OK",
	})
	return
}
